package juc.day01;

import java.util.concurrent.atomic.AtomicInteger;

/*
1.i++的原子性问题：i++实际上分为三个步骤：读-改-写
    int i = 10;
    i = i++;//10

    int temp = i;
    i = i+1;
    i = temp;

    i = i+1;
    int temp = i;
    i = temp;

2.原子变量：jdk1.5之后java.util.concurrent.atomic包下提供了常用的原子变量
        volitile保证内存可见性
        CAS(Compare-And-Swap)算法保证数据原子性
            CAS算法是硬件对于并发操作共享数据的支持
            CAS包含了三个操作数
                内存值V
                预估值A
                更新值B
                当且仅当V==A时，V = B，否则，将不做任何操作
  */
public class TestAtomicDemo {

    public static void main(String[] args) {
        Atomic a = new Atomic();
        for (int i = 0; i < 10; i++) {
            new Thread(a).start();
        }
    }
}

class Atomic implements Runnable {
    //    private int serialNumber = 0;
    private AtomicInteger serialNumber = new AtomicInteger(0);


    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(getSerialNumber());
    }

    public int getSerialNumber() {
        return serialNumber.getAndIncrement();
    }


}
